package com.atguigu.edu.controller;


import com.atguigu.edu.entity.EduTeacher;
import com.atguigu.edu.service.EduTeacherService;
import com.atguigu.request.EduTeacherConditionVO;
import com.atguigu.response.RetVal;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.bcel.internal.generic.RET;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author HB
 * @since 2020-11-01
 * @CrossOrigin解决跨域问题
 */
@RestController
@RequestMapping("/edu/teacher")
@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;
    //查询所有讲师

    /**
     * 测试地址：http://localhost:8000/edu/teacher
     * @return
     */
//    @GetMapping
//    public List<EduTeacher> queryAllEduTeacher(){
//        List<EduTeacher> list = eduTeacherService.list(null);
//        for (EduTeacher eduTeacher : list) {
//            System.out.println("eduTeacher = " + eduTeacher);
//        }
//        return  list;
//    }

    /**
     * 删除讲师
     * 测试地址：http://localhost:8000/edu/teacher/3
     * 注意需要发送的是delete请求，在postman中发送
     */
//    @DeleteMapping("{id}")
//    public boolean deleteEduTeacherById(@ApiParam(name = "id",value = "讲师id",required = true) @PathVariable String id){
//        return eduTeacherService.removeById(id);
//    }
    /**
     * 添加讲师
     *
     * @return
     */
    @PostMapping
    public RetVal insertEduTeacher(EduTeacher eduTeacher) {
        boolean flag = eduTeacherService.save(eduTeacher);
        if (flag){
            //添加成功
            return RetVal.success();
        }else {
            //添加失败
            return RetVal.error();
        }
    }

    /**
     * 删除讲师
     * 测试地址：http://localhost:8000/edu/teacher/3
     * 注意需要发送的是delete请求，在postman中发送
     * 已实现逻辑删除
     */
    @DeleteMapping("{id}")
    public RetVal deleteEduTeacherById(@ApiParam(name = "id", value = "讲师id", required = true) @PathVariable String id) {
        boolean flag = eduTeacherService.removeById(id);
        if (flag) {
            return RetVal.success();
        } else {
            return RetVal.error();
        }
    }

    /**
     * 修改讲师的信息
     */
    @PutMapping
    public RetVal updateEduTeacher(EduTeacher eduTeacher){
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if(flag){
            return RetVal.success();
        }else{
            return RetVal.error();
        }
    }



    /**
     * 查询所有讲师
     * 测试地址：http://localhost:8000/edu/teacher
     *
     * @return
     */
    @GetMapping
    public RetVal queryAllEduTeacher() {
        //模拟异常
        //int a=1/0;
        List<EduTeacher> teacherList = eduTeacherService.list(null);
        return RetVal.success().data("teacherList", teacherList);
    }

    /**
     * 通过id查询讲师
     */
    @GetMapping("{id}")
    public RetVal queryEduTeacherById(@PathVariable String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        return RetVal.success().data("teacher",teacher);
    }




    /**
     * 无条件分页查询所有讲师
     *
     * @param pageNumber 两个参数一定要是long类型的 因为new Page()的构造函数时需要long类型的参数
     * @param pageSize
     * @return
     */
    @GetMapping("paginationQueryEduTeacher/{pageNumber}/{pageSize}")
    public RetVal paginationQueryEduTeacher(@PathVariable("pageNumber") long pageNumber, @PathVariable("pageSize") long pageSize) {
        Page<EduTeacher> page = new Page<>(pageNumber, pageSize);
//        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        eduTeacherService.page(page, null);
        //总记录数
        long total = page.getTotal();
        //总结果集
        List<EduTeacher> teacherList = page.getRecords();

        return RetVal.success().data("total", total).data("teacherList", teacherList);
    }

    /**
     * @param pageNumber            当前页
     * @param pageSize              当前页的大小
     * @param eduTeacherConditionVO 封装的条件
     * @return
     */
    @GetMapping("paginationQueryEduTeacherByCondition/{pageNumber}/{pageSize}")
    public RetVal paginationQueryEduTeacherByCondition
    (@PathVariable("pageNumber") long pageNumber,
     @PathVariable("pageSize") long pageSize,
     EduTeacherConditionVO eduTeacherConditionVO) {
        Page<EduTeacher> teacherPage = new Page<>(pageNumber, pageSize);

        eduTeacherService.queryTeacherPageByCondition(teacherPage, eduTeacherConditionVO);
        //总记录数
        long total = teacherPage.getTotal();
        //总结果集
        //小坑：如果是后台数据查找出来了 前台显示不了数据 大多数是因为后台返回给前台的数据的名字没有对上
        // 例如这里的teacherList 前台显示数据的名字也要是teacherList
        List<EduTeacher> teacherList = teacherPage.getRecords();
        return RetVal.success().data("total", total).data("teacherList", teacherList);
    }
}

